/**
 * 中国9号健康产业有限公司
 * Copyright (C) JiuHaoHealth Corporation. All Rights Reserved
 */
package com.jiuhaohealth.kangxun360.account.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jiuhaohealth.common.page.PageModel;
import com.jiuhaohealth.kangxun360.account.AccountDao;
import com.jiuhaohealth.kangxun360.account.AccountService;
import com.jiuhaohealth.kangxun360.constants.CacheName;
import com.jiuhaohealth.kangxun360.main.po.Account;
import com.jiuhaohealth.kangxun360.main.po.AccountKey;
/**
 * @author minghui(jackchenmh@gmail.com)
 * @version 2014-3-18 下午5:08:45
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

	@Resource(name="accountDao")
	private AccountDao accountDao;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
	public Integer deleteAccountByPk(Integer id, String userId) {
		AccountKey accountKey = new AccountKey();
		accountKey.setId(id);
		accountKey.setUserid(userId);
		return accountDao.deleteAccountById(accountKey);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
	public Integer modifyAccount(Account account) {
		return accountDao.updateAccount(account);
	}

	@Override
	@Cacheable(value={CacheName.ACCOUNT_INFO}, key="'account_info' + #id+'_'+#userId")
	public Account queryAccountByPk(Integer id, String userId) {
		AccountKey accountKey = new AccountKey();
		accountKey.setId(id);
		accountKey.setUserid(userId);
		return accountDao.selectAccountByPk(accountKey);
	}

	@Override
	public List<Account> queryAccounts(String firstName, String lastName) {
		return accountDao.selectAccounts(firstName, lastName);
	}

	@Override
	public List<Account> queryAccounts(String firstName, Integer pageSize, Integer pageNo) {
		return accountDao.selectAccounts(firstName, pageSize, pageNo);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
	public Integer saveAccount(Account account) {
		return accountDao.insertAccount(account);
	}

	@Override
	public PageModel<Account> listAccount(String userId, int offset, int pageSize) {
		PageModel<Account>  model = new PageModel<Account>();
		List<Account> accounts = accountDao.selectAccounts(userId, pageSize, offset);
		int totalNumber = accountDao.countByUserId(userId);
		model.setData(accounts);
		model.setTotalNumber(totalNumber);
		model.setOffset(offset);
		model.setPageSize(pageSize);
		return model;
	}

}
